SetGfxDriver 1:Graphics 340,60,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global nums,numw,prostir,cfg_hmn,cfg_tgt
prostir=4096
nums = CreateBank(prostir+2):numw = CreateBank(prostir+2)
For i = 0 To prostir-1 
t = Rand(0,255):PokeByte nums,i, t:PokeByte numw,i, t 
Next 
entropy():hmn=findhuman(0):tgt=findtarget(hmn):itrcnt=0:stopflag=False:stopflag2=False

While Not KeyHit(1) 
Cls

If stopflag=False iteration(hmn,tgt):hmn=cfg_hmn:tgt=cfg_tgt:Color 0,255,0:Text 170,10,"itrcnt: "+itrcnt,True ,True:itrcnt = itrcnt +1
If itrcnt>1 And stopflag=False stopflag=True:dhmn=hmn:dtgt=tgt:untropy()
;If itrcnt>=0 And stopflag=True deiteration(dhmn,dtgt):dhmn=cfg_hmn:dtgt=cfg_tgt:Color 0,255,0:Text 170,10,"itrcnt: "+itrcnt,True ,True:itrcnt = itrcnt -1
;If stopflag2=False And intrcnt<0 stopflag=True: compire()


Flip 
If KeyHit(57) > 0 WaitMouse()
Wend 


Function deiteration(hmn,tgt)
;demovehuman(hmn,tgt):
tgt = mod2(tgt+1): 
;hmn=defindhuman(tgt):tgt=defindtarget(hmn)
cfg_hmn=hmn:cfg_tgt=tgt
End Function

Function iteration(hmn,tgt)
movehuman(hmn,tgt):tgt = mod2(tgt+1): hmn=findhuman(tgt):tgt=findtarget(hmn)
cfg_hmn=hmn:cfg_tgt=tgt
End Function

Function movehuman(hmn,tgt)
current=PeekByte (nums,hmn)
If tgt<hmn Then
   For i=hmn To prostir-2
      PokeByte nums,i,PeekByte (nums,mod2(i+1))
   Next
   PokeByte nums,prostir-1,PeekByte (nums,0);-------------
   For i=0 To tgt-1
      PokeByte nums,i,PeekByte (nums,mod2(i+1))
   Next
      PokeByte nums,tgt,current
Else If tgt>hmn
   For i=hmn To tgt-1
    If tgt<prostir-2 And hmn>2 And tgt>hmn Then
          PokeByte nums,i,(PeekByte (nums,mod2(i+1)) Xor 255) 
        Else
          PokeByte nums,i,PeekByte (nums,mod2(i+1))
       EndIf
   Next
If z1<prostir-2 And z>2 And tgt>hmn Then PokeByte nums,tgt,(current Xor 255) Else PokeByte nums,tgt,current
EndIf
End Function

Function findtarget(z)
PokeByte nums,prostir,PeekByte (nums,0):PokeByte nums,prostir+1,PeekByte (nums,1)
current = PeekByte (nums,z)
If PeekByte (nums,z)<PeekByte (nums,mod2(z+1)) za1 = 0 Else za1=1
If PeekByte (nums,z)<PeekByte (nums,mod2(z+2)) za2 = 0 Else za2=1
If PeekByte (nums,mod2(z+1))<PeekByte (nums,mod2(z+2)) za3 = 0 Else za3=1
If PeekByte (nums,z)=PeekByte (nums,mod2(z+1)) za1 = 2
If PeekByte (nums,z)=PeekByte (nums,mod2(z+2)) za2 = 2
If PeekByte (nums,mod2(z+1))=PeekByte (nums,mod2(z+2)) za3 = 2
For i =z+1 To prostir-1
If PeekByte (nums,i)<PeekByte (nums,mod2(i+1)) a1 = 0 Else a1=1
If PeekByte (nums,i)<PeekByte (nums,mod2(i+2)) a2 = 0 Else a2=1
If PeekByte (nums,mod2(i+1))<PeekByte (nums,mod2(i+2)) a3 = 0 Else a3=1
If PeekByte (nums,i)=PeekByte (nums,mod2(i+1)) a1 = 2
If PeekByte (nums,i)=PeekByte (nums,mod2(i+2)) a2 = 2
If PeekByte (nums,mod2(i+1))=PeekByte (nums,mod2(i+2)) a3 = 2
If(a1=za1 And a2=za2 And a3=za3) Then z1=mod2(i):i=prostir:finded=True Else finded=False 
Next
If finded=False Then
For i =0 To z-1
If PeekByte (nums,i)<PeekByte (nums,mod2(i+1)) a1 = 0 Else a1=1
If PeekByte (nums,i)<PeekByte (nums,mod2(i+2)) a2 = 0 Else a2=1
If PeekByte (nums,mod2(i+1))<PeekByte (nums,mod2(i+2)) a3 = 0 Else a3=1
If PeekByte (nums,i)=PeekByte (nums,mod2(i+1)) a1 = 2
If PeekByte (nums,i)=PeekByte (nums,mod2(i+2)) a2 = 2
If PeekByte (nums,mod2(i+1))=PeekByte (nums,mod2(i+2)) a3 = 2
If(a1=za1 And a2=za2 And a3=za3) Then z1=mod2(i):i=prostir:finded=True Else finded=False 
Next
EndIf
Return z1
End Function

Function findhuman(z)
For i=z To prostir-1
  If PeekByte (nums,i)>127 Then z=i:i=prostir:finded=True Else finded=False 
Next
If finded=False Then
For i=0 To z
 If PeekByte (nums,i)>127 Then z=i:i=prostir:finded=True Else finded=False 
Next
EndIf
Return z
End Function

Function compire()
errors = 0
For i=0 To prostir-1
  If PeekByte (nums,i)<>PeekByte(numw,i) errors=errors+1: i=prostir
Next
Color 0,255,0:Text 170,20,"errors: "+errors,True ,True

End Function

Function entropy()
fileBank=WriteFile("c:\1\teorema.in") 
WriteBytes nums,fileBank,0,prostir
CloseFile fileBank 
End Function

Function untropy()
fileBank=WriteFile(("c:\1\teorema.out")) 
WriteBytes nums,fileBank,0,prostir
CloseFile fileBank 
CopyBank nums,0,numw,0,prostir
fileout = WriteFile("c:\1\teorema.dcl")
cfg$=  String$(" "+String$(cfg_hmn,1)+" hmn, "+String$(cfg_tgt,1)+" tgt ",1)
SeekFile( fileout, 0 ) 
cstr = CreateBank(Len(cfg$))
For i=1 To Len(cfg$)
PokeByte cstr,i,Asc(Mid$(cfg$,i,1))
Next
WriteBytes( cstr,fileout, 1,Len(cfg$)) 
CloseFile( fileout )
FreeBank cstr 
End Function

Function mod2(modx)
If modx<0 Return modx=prostir-2-modx Else Return (modx Mod (prostir-2))
End Function